package cn.fastloader.util.SMB;

import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import jcifs.smb.SmbSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SMBUtils {
    private static final Logger log = LogManager.getLogger(SMBUtils.class);
    /**
     * 往samba上传文件
     * @param url       服务器IP地址
     * @param userName  用户登录名
     * @param password  用户登录密码
     * @param storePath 服务器文件存储路径
     * @param fileName  服务器文件存储名称
     * @param is        文件输入流
     * @return true：上传成功
     *

     * false：上传失败
     * */
    public static boolean storeFile(String url, String userName, String password, String storePath, String fileName, InputStream is) {
        BufferedInputStream bf = null;
        OutputStream os = null;
      boolean result = false;
        try {
            //UniAddress ua = UniAddress.getByName(url);
            //NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url, userName, password);
            //SmbSession.logon(ua, auth);
            //SmbFile sf = new SmbFile("smb://" + url + storePath+"/"+fileName, auth);
            //SmbFile sffolder = new SmbFile("smb://" + url + storePath, auth);
            //SmbFile sf = new SmbFile("smb://Wyh:Admin@jsyl1@10.198.9.81/共享/"+ storePath+"/"+fileName);
            //SmbFile sffolder = new SmbFile("smb://Wyh:Admin@jsyl1@10.198.9.81/共享/" + storePath);
            SmbFile sf = new SmbFile("smb://GWT:Wjb85161002@10.198.9.88/共享文件/"+ storePath+"/"+fileName);
            SmbFile sffolder = new SmbFile("smb://Wyh:Admin@jsyl1@10.198.9.81/共享/" + storePath);
            System.out.println(sf.isDirectory());
            if(!sffolder.exists())
                sffolder.mkdirs();
            bf = new BufferedInputStream(is);
            //os = sf.getOutputStream();
            os = new SmbFileOutputStream(sf);
            byte[] bt = new byte[1024];
            int n = bf.read(bt);
            while (n != -1){
                os.write(bt, 0, n);
                os.flush();
                n = bf.read(bt);
            }
//            IOUtils.copy(is, sf.getOutputStream());
            result = true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }finally {
            try {
                if(bf!=null)
                    bf.close();
                if(os!=null)
                    os.close();
            } catch (IOException e) {
                log.error(e.getMessage(),e);
            }
        }
        return result;
    }
    /**
     * 从samba下载文件
     * @param url        服务器IP地址
     * @param userName   用户登录名
     * @param password   用户登录密码
     * @param remotePath 服务器文件存储路径
     * @param fileName   服务器文件存储名称
     * @param os  文件输出流
     * @return true：下载成功
     *

     * false：下载失败
     * */
    public static boolean retrieveFile(String url, String userName, String password, String remotePath, String fileName, OutputStream os) {
        boolean result = false;
        BufferedInputStream bf = null;
        try {
            UniAddress ua = UniAddress.getByName(url);
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url, userName, password);
            SmbSession.logon(ua, auth);
            SmbFile sf = new SmbFile("smb://"+ url + remotePath+"/"+fileName, auth);
//            SmbFile sf = new SmbFile("smb://" +userName+":"+password+"@"+ url + remotePath+"/"+fileName, auth);
            if(sf.exists()) {
                bf = new BufferedInputStream(sf.getInputStream());
                byte[] bt = new byte[1024];
                int n = bf.read(bt);
                while (n != -1){
                    os.write(bt, 0, n);
                    os.flush();
                    n = bf.read(bt);
                }
//                IOUtils.copy(sf.getInputStream(), os);
                result = true;
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }finally {
            try {
                if(bf!=null)
                    bf.close();
                if(os!=null)
                    os.close();
            } catch (IOException e) {
                log.error(e.getMessage(),e);
            }
        }
        return result;
    }
    /**
     * 从samba删除文件
     * @param url        服务器IP地址
     * @param userName   用户登录名
     * @param password   用户登录密码
     * @param remotePath 服务器文件存储路径
     * @param fileName   服务器文件存储名称
     * @return true：删除成功
     *

     * false：删除失败
     * */
    public static boolean deleteFile(String url, String userName, String password, String remotePath, String fileName) {
        boolean result = false;
        try {
            UniAddress ua = UniAddress.getByName(url);
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url, userName, password);
            SmbSession.logon(ua, auth);
            SmbFile sf = new SmbFile("smb://" + url + remotePath+"/"+fileName, auth);
            sf.delete();
            result = true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }
    /**
     * 往samba上传文件
     * @param smbConfig
     * @return
     * */
    public static boolean storeFile(SMBConfig smbConfig) {
        return storeFile(smbConfig.getSmbip(), smbConfig.getSmbuser(), smbConfig.getSmbps(), smbConfig.getSmbpath(), smbConfig.getSmbfile(), smbConfig.getIs());
    }
    /**
     * 从samba下载文件
     * @param smbConfig
     * @return
     * */
    public static boolean retrieveFile(SMBConfig smbConfig) {
        return retrieveFile(smbConfig.getSmbip(), smbConfig.getSmbuser(), smbConfig.getSmbps(), smbConfig.getSmbpath(), smbConfig.getSmbfile(), smbConfig.getOs());
    }
}
